package com.giscloud.amp.service.auth.impl;

import com.giscloud.amp.dao.AmpSysMapMapper;
import com.giscloud.amp.entity.AmpSysMap;
import com.giscloud.amp.entity.dto.AmpSysMapDTO;
import com.giscloud.amp.service.auth.AmpSysMapService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Author:gxd
 * @Description: 权限服务类
 * @Date: 17:19 2018/2/24
 * @Modified By:
 */
@Service("ampSysMapService")
@Transactional(propagation= Propagation.REQUIRED, rollbackFor=Exception.class)
public class AmpSysMapServiceImpl implements AmpSysMapService {
    private final static Logger log = LoggerFactory.getLogger(AmpSysMapServiceImpl.class);
    @Autowired
    private AmpSysMapMapper ampSysMapMapper;

     @Override
    public List<Long> findSysMapBySystemAndCity(AmpSysMapDTO ampSysMapDTO) {
        AmpSysMap ampSysMap = new AmpSysMap();
        BeanUtils.copyProperties(ampSysMapDTO,ampSysMap);
        return ampSysMapMapper.findSysMapBySystemAndCity(ampSysMap,ampSysMapDTO.getCityId());
    }

     @Override
    public int save(AmpSysMapDTO ampSysMapDTO) {
        AmpSysMap ampSysMap = new AmpSysMap();
        BeanUtils.copyProperties(ampSysMapDTO,ampSysMap);
        ampSysMap.setCreatedDt(new Date());
        ampSysMap.setVersion(new Date());
        ampSysMap.setStatusCd(1);
        int j = 0;
        if(ampSysMapDTO.getCheckMapIds()!=null&&ampSysMapDTO.getCheckMapIds().size()>0){
            for (int i = 0; i < ampSysMapDTO.getCheckMapIds().size(); i++) {
                ampSysMap.setMapId(ampSysMapDTO.getCheckMapIds().get(i));
                j+=insert(ampSysMap);
            }
        }
        ampSysMap.setStatusCd(2);
        if(ampSysMapDTO.getUnCheckMapIds()!=null&&ampSysMapDTO.getUnCheckMapIds().size()>0){
            for (int i = 0; i < ampSysMapDTO.getUnCheckMapIds().size(); i++) {
                ampSysMap.setMapId(ampSysMapDTO.getUnCheckMapIds().get(i));
                j+=delete(ampSysMap);
            }
        }
        return j;
    }

    @Override
    public int insert(AmpSysMap ampSysMap) {
        int j = 0;
        if(ampSysMapMapper.findSysMapCountBySystemAndMap(ampSysMap)==0){
            j = ampSysMapMapper.insert(ampSysMap);
        }
        return j;
    }

    @Override
    public int delete(AmpSysMap ampSysMap) {
        int j = 0;
        if(ampSysMapMapper.findSysMapCountBySystemAndMap(ampSysMap)>0){
            j = ampSysMapMapper.delete(ampSysMap);
        }
        return j;
    }

}
